var register = new Vue({
    el: '#app-register',
    data: {
        //表单信息
        phone:"",
        vft_code:"",
        nickname:"",
        password:"",
        passwordAgain:"",
        btn_vft:"获取验证码",

        //错误提示
        showAlertMsg:false,
        alertMsg:"内容有误",

        count:60
    },
    methods: {
        //获取验证码
        getSMSCode: function () {
            var btn_sms = document.getElementById("btn");
            var t;
            if (this.checkPhone()) {
                console.log("check success");
                if (this.count === 60) {
                    console.log(this.count);
                    console.log(this.phone);
                    Bmob.initialize("8fede9808606d9e527239509b616fcfa", "6d5b70c5d7f464beca9fda0ccd5dc89b");
                    Bmob.Sms.requestSmsCode({"mobilePhoneNumber": this.phone, "template": "T1"}).then((obj) => {
                        btn_sms.setAttribute("style", "color:#aaa");
                        t = setInterval(function () {
                            console.log(register.count);
                            register.btn_vft = "获取验证码(" + register.count + ")";
                            register.count--;
                            console.log(register.count);
                            if (register.count <= 0) {
                                clearInterval(t);
                                register.count = 60;
                                register.btn_vft = "获取验证码";
                                btn_sms.setAttribute("style", "color:#f6007d");
                            }
                        }, 1000);
                    }, (err) => {
                        console.log("获取失败:"+err.data);
                        register.showAlert("获取验证码失败");
                    });
                }
            } else {
                console.log("判断出错");
                this.showAlert("手机号错误");
            }
        },

        //注册
        register: function () {
            if (this.checkPhone() && this.checkVft() && this.checkPwd()) {
                //验证密码
                Bmob.Sms.verifySmsCode(this.phone, this.vft_code).then(function (obj) {
                    console.log("验证成功");
                    //注册
                    this.$http.post("http://120.77.223.88:8080/red/Register", {
                        phone: register.phone,
                        password: register.password,
                        nickname: register.nickname
                    }, {emulateJSON: true})
                        .then((res) => {
                            console.log(res.data);
                            if (res.data.status == 1) {
                                //注册成功，自动登录，存储Cookies
                                window.localStorage.setItem('isLogin', true);
                                window.localStorage.setItem('nickname', register.nickname);
                                window.localStorage.setItem('phone', register.phone);
                                window.localStorage.setItem('password', register.password);
                                alert("注册成功");
                                //返回
                                window.history.back(-1);
                                //刷新
                                window.location.reload(true);
                            } else if(res.data.status == 0){
                                console.log("用户已存在");
                                this.showAlert("用户已存在");
                            } else {
                                alert("注册失败");
                            }
                        }, (res) => {
                            alert("注册失败: " + res);
                        });
                }, function (err) {
                    console.log(err);
                    this.showAlert_P("验证码错误");
                });
            }
        },

        //动态检测输入是否正确
        checkPhone: function () {
            if (!(/^[1][3,4,5,7,8][0-9]{9}$/.test(this.phone))) {
                this.showAlert("手机号错误");
                return false;
            } else {
                this.hideAlert();
                return true;
            }
        },

        checkVft: function () {
            if (this.vft_code.length !== 6) {
                this.showAlert("验证码错误");
                return false;
            } else {
                this.hideAlert();
                return true;
            }

        },

        checkPwd: function () {
            if(this.password !== this.passwordAgain){
                this.showAlert("前后密码不等");
                return false;
            } else {
                this.hideAlert();
                return true;
            }
        },

        // 显示隐藏错误信息
        showAlert: function (str) {
            this.alertMsg = str;
            this.showAlertMsg = true;
        },

        hideAlert: function () {
            this.showAlertMsg = false;
        }
    }
});